<?php
/**
 * 毛文科
 */
namespace app\admin\controller;
use app\admin\model\MyChildren;
use app\admin\model\MySubject;
use builder\Builder;
use think\Db;
use think\Request;

class Lianbiao
{
    public function show_list(Request $request){
        $model_id = $request->param('model_id');
        $obj = new MySubject();
        $obj->setModelId($model_id);
        $model = $obj->getModel();
        $field = $obj->getField();
//        echo '<pre>';
//        print_r($field);
//        exit;
        $builder = Builder::table()
            ->setAction('lianbiao/page?model_id='.$model_id)
            ->setModelId($model_id)
            ->setMonitorAction('switch','lianbiao/change')
            ->setMonitorAction('edit','lianbiao/change')
            ->setRightHeader();
//        echo '<pre>';
//        print_r($model['right']);
//        exit;
        foreach ($model['toops'] as $value){
            if($value=='alldel'){
                $builder = $builder->addToolbarDemo('alldel','全部删除',[],'lianbiao/del');
            }
            if($value=='add'){
                $builder = $builder->addToolbarDemo('add','添加',[],'lianbiao/show_add?model_id='.$model_id);
            }
            if($value=='back'){
                $builder = $builder->addToolbarDemo('back','返回');
            }
            if($value=='search'){
                $newfield = [];
                foreach ($field as $value){
                    if($value['search']==1){
                        $newfield[] = $value;
                    }
                }
                $builder = $builder->addToolbarDemo('search','搜索',$newfield);
            }
        }
        foreach ($field as $value){
            if($value['show']==1){
                if($value['form_type']=='单行文本'||$value['form_type']=='多行文本'||$value['form_type']=='下拉框'||$value['form_type']=='联动下拉框'||$value['form_type']=='编辑框'||$value['form_type']=='单选框'||$value['form_type']=='多选框'||$value['form_type']=='联动单选框'||$value['form_type']=='联动多选框'||$value['form_type']=='联动下拉组'){
                    $builder = $builder->setColumn('input',$value['field'],$value['field_name'],'lianbiao/change?model_id='.$model_id);
                }
                if($value['form_type']=='时间'){
                    $builder = $builder->setColumn('input',$value['field'],$value['field_name']);
                }
                if($value['form_type']=='单图片'){
                    $builder = $builder->setColumn('img',$value['field'],$value['field_name']);
                }
                if($value['form_type']=='多图片'){
                    $builder = $builder->setColumn('imgs',$value['field'],$value['field_name']);
                }
                if($value['form_type']=='开关'){
                    if($value['option']){
                        $array = explode(',',$value['option']);
                    }else{
                        $array = ['开启','关闭'];
                    }
                    $builder = $builder->setColumn('switch',$value['field'],$value['field_name'],'lianbiao/change?model_id='.$model_id,null,'center','',$array);
                }
            }
        }
        foreach ($model['right'] as $value){
            if($value=='edit'){
                $builder->setButton($value,'编辑','lianbiao/show_edit?model_id='.$model_id);
            }
            if($value=='del'){
                $builder->setButton($value,'删除','lianbiao/del?model_id='.$model_id);
            }
            if($value=='add'){
                $builder->setButton($value,'添加','lianbiao/show_add?model_id='.$model_id);
            }
            if($value=='detail'){
                $builder->setButton($value,'查看','lianbiao/detail?model_id='.$model_id);
            }
            if($value=='a'){
                $builder->setButton($value,'编辑字段','lianbiao/show_list?model_id='.$model_id);
            }
        }
        return $builder->setPage()
            ->fetch();
    }
    public function page(Request $request){
        $model_id = $request->param('model_id');
        $limit = $request->param('limit',10);
        $page = $request->param('page',1);
        $fields = $request->param('field',null);
        $tiaojian = $request->param('tiaojian',null);
        $obj = new MySubject();
        $obj->setModelId($model_id);
        $table = $obj->getModel('table');
        $field = $obj->getField();
        $where = [];
        if($fields){
            if($fields!=='all'){
                foreach ($field as $value){
                    if($value['field']==$fields){
                        if($value['form_type']=='单行文本'||$value['form_type']=='多行文本'){
                            $where[$fields] = $tiaojian;
                        }
                    }
                }
            }
        }else{
            $where['name'] = $tiaojian;
        }
        $data =MyChildren::getDatas($table,false,$where);
        $respos['total'] = count($data);
        $data = array_slice($data,($page-1)*$limit,$limit);
        foreach ($data as $key=>$value){
            foreach ($field as $va){
                if($va['form_type']=='时间'){
                    $value[$va['field']] = date('Y-m-d',$value[$va['field']]);
                }
                if($va['form_type']=='单图片'){
                    $value[$va['field']] = getFilePath($value[$va['field']]);
                }
                if($va['form_type']=='多图片'){
                    $value[$va['field']] = getFilePath($value[$va['field']],false);
                }
                if($va['form_type']=='联动下拉框'||$va['form_type']=='联动单选框'){
                    if(!$value[$va['field']]){
                        $value[$va['field']] = '最上级';
                        continue;
                    }
                    $table = Db::name(getTable('models'))
                        ->where('id',$va['table'])
                        ->find();
                    $shu = Db::name($table['table'])
                        ->where('id',$value[$va['field']])
                        ->find();
                    $value[$va['field']] = $shu['name'];
                }

                if($va['form_type']=='联动下拉组'){
                    $table1 = explode(',',$va['table']);
                    $table1 = Db::name(getTable('models'))->where('id',$table1[1])->find();
                    $mydata = Db::name($table1['table'])->where('id',$value[$va['field']])->where('status',1)->field('name')->find();
                    if($mydata){
                        $value[$va['field']] = $mydata['name'];
                    }else{
                        $value[$va['field']] = '';
                    }
                }
            }
            $data[$key] = $value;
        }
        $respos['data'] = $data;
//        echo '<pre>';
//        print_r($data);
//        exit;
        return json_page_response($respos);
    }
    public function show_add(Request $request){
        $data = $request->param();
//        echo '<pre>';
//        print_r($data);
//        exit;
        $obj = new MySubject();
        $obj->setModelId($data['model_id']);
        $field = $obj->getField();
//
//        if($data['model_id'])
//                echo '<pre>';
//        print_r($field);
//        exit;
        if($data['id']??null){
            return $obj->getForm('lianbiao/add?model_id='.$data['model_id'],null,true,[],$data['id']);
        }else{
            return $obj->getForm('lianbiao/add?model_id='.$data['model_id']);
        }
    }
    public function change(Request $request){
        $data = $request->param();
        $obj = new MySubject();
        $obj->setModelId($data['model_id']);
        $table = $obj->getModel('table');
        $datas = Db::name($table)
            ->where('id',$data['id'])
            ->find();
        if($datas[$data['field']]==1){
            $datas[$data['field']]=0;
        }else{
            $datas[$data['field']]=1;
        }
        if(Db::name($table)->where('id',$data['id'])->update($datas)){
            return json_to_respos('修改成功');
        }else{
            return json_to_respos('修改失败',205);
        }
    }
    public function add(Request $request){
        $data = $request->param();
//        echo '<pre>';
//        print_r($data);
//        exit;
        $obj = new MySubject();
        $obj->setModelId($data['model_id']);
        $data = $obj->getData($data);
        $table = $obj->getModel('table');
        if(Db::name($table)->insert($data,true)){
            return redirect('index/errors',['code'=>200]);
        }else{
            return redirect('index/errors',['code'=>205]);
        }
    }
    public function show_edit(Request $request){
        $data = $request->param();
//        echo '<pre>';
//        print_r($data);
//        exit;
        $obj = new MySubject();
        $obj->setModelId($data['model_id']);
//        $field = $obj->getField();
//
//        if($data['model_id'])
//                echo '<pre>';
//        print_r($field);
//        exit;
        if($data['id']??null){
            return $obj->getForm('lianbiao/edit?model_id='.$data['model_id'],$data['id'],true,[],$data['id']);
        }else{
            return $obj->getForm('lianbiao/edit?model_id='.$data['model_id']);
        }
    }
    public function edit(Request $request){
        $data = $request->param();
        $obj = new MySubject();
        $obj->setModelId($data['model_id']);
        $data = $obj->getData($data);
        $table = $obj->getModel('table');
        if(Db::name($table)->where('id',$data['id'])->update($data)){
            return redirect('index/errors',['code'=>200]);
        }else{
            return redirect('index/errors',['code'=>205]);
        }
    }
    public function detail(Request $request){
        $data = $request->param();
        $obj = new MySubject();
        $obj->setModelId($data['model_id']);
//        $model = $obj->getModel();
//
        return $obj->getForm('typeone/edit?model_id='.$data['model_id'],$data['id'],false);
    }
    public function del(Request $request){
        $data = $request->param();
        $obj = new MySubject();
        $obj->setModelId($data['model_id']);
        $table = $obj->getModel('table');
        $newdata = Db::name($table)->where('id','in',$data['id'])->select();
        foreach ($newdata as $value){
            if($value['delete']!==1){
                return json_to_respos('有不可删除字段',205);
            }
        }
        if(Db::name($table)->where('id','in',$data['id'])->delete()){
            return json_to_respos('删除成功');
        }else{
            return json_to_respos('删除失败',205);
        }
    }

}